/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.foundation.chart;

import com.intelligent.ispc.common.utils.StringUtils;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;

/**
 * Created by Peter on 2016/8/4.
 */
public abstract class AbstractChart implements IChart {

    private String digNumFormat = "0.0000";
    private int digNum = 4;

    @Override
    public ChartPanel createChartPanel() {
        ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
        JFreeChart chart = buildChart();
        ChartUtilities.applyCurrentTheme(chart);
        return new ChartPanel(chart);
    }

    /**
     * Init the format for dig space.
     *
     * @param number space number.
     */
    public void initDigNumFormat(int number) {
        digNumFormat = StringUtils.formatDouble(0D, number);
        digNum = number;
    }

    /**
     * Build chart.
     *
     * @return a chart instance.
     */
    protected abstract JFreeChart buildChart();

    /**
     * Return a blank chart.
     *
     * @param chartName chart name.
     * @return chart instance.
     */
    public JFreeChart blankChart(String chartName) {
        JFreeChart chart = ChartFactory.createXYLineChart(chartName, "", "", null, PlotOrientation.VERTICAL, false, false, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.getDomainAxis().setVisible(false);
        plot.getRangeAxis().setVisible(false);
        return chart;
    }

    public String getDigNumFormat() {
        return digNumFormat;
    }

    public int getDigNum() {
        return digNum;
    }
}
